package controllers.chat;

import controllers.Application;
import models.ApplicationUser;
import models.chat.Channel;
import models.chat.Status;
import models.chat.engines.MessageEngine;
import models.chat.messages.Message;
import models.chat.messages.TimePeriod;
import models.chat.messages.system.MemberChangedStatusMessage;
import models.chat.messages.system.MemberJoinedChannelMessage;
import play.mvc.Before;
import play.mvc.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Konovalov_Nik
 */
public class ChatController extends Controller {

    public static final String GLOBAL = "Global";

    @Before
    static void checkAuthenticated() {
        if (!session.contains("uid")) {
            session.clear();
            Application.login();
        }
        ApplicationUser user = ApplicationUser.findById(Long.parseLong(session.get("uid")));
        if (user == null) {
            session.clear();
            Application.login();
        }
    }

    public static void index(String name) {
        String uid = session.get("uid");
        ApplicationUser current = ApplicationUser.findById(Long.valueOf(uid));

        Channel channel;
        if (name == null) {
            channel = Channel.find("byName", GLOBAL).first();
        } else {
            //name is User name
            ApplicationUser member = ApplicationUser.find("byName", name).first();

            String name1 = String.valueOf(current.id);
            String name2 = String.valueOf(member.id);
            if (name1.compareTo(name2) == -1) {
                name1 = String.valueOf(member.id);
                name2 = String.valueOf(current.id);
            }
            String channelName;
            if (name1.equals(name2)) {
                channelName = GLOBAL;
            } else {
                channelName = "pr-" + name1 + "-" + name2;
            }
            channel = Channel.find("byName", channelName).first();

        }
        MessageEngine.addMessage(new MemberJoinedChannelMessage(current, channel));

        render(current, channel);
    }

    public static void listMembers(String channelName) {
        Channel channel = Channel.find("byName", channelName).first();
        List<ApplicationUser> members = channel.getMembers();
        render(members);
    }

    public static void fetchMessages(Long channelId, TimePeriod period) {
        Channel channel = Channel.findById(channelId);
        Map<Long, String> idToName = new HashMap<Long, String>();
        List<ApplicationUser> allMembers = ApplicationUser.findAll();
        for (ApplicationUser user : allMembers) {
            idToName.put(user.id, user.name);
        }
        if (channel != null) {
            List<Message> historyMessages = channel.loadMessagesForPeriod(period);
            render(historyMessages, idToName);
        } else {
            render("Cant fetch messages");
        }
    }


    public static void renameMember(String newName) {
        String uid = session.get("uid");
        ApplicationUser current = ApplicationUser.findById(Long.parseLong(uid));
        current.name = newName;
        current.save();
    }

    public static void changeStatus(String status) {
        String uid = session.get("uid");
        ApplicationUser current = ApplicationUser.findById(Long.parseLong(uid));
        MessageEngine.addMessage(new MemberChangedStatusMessage(current, Status.valueOf(status)));
    }
}
